package utils.db.impl;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



import utils.db.api.IEditAccount;

public class EditAccount extends DBOperation implements IEditAccount{
	private static Logger logger = LoggerFactory.getLogger(EditAccount.class);
	
	public EditAccount(){
		super();
	}
	
	@Override
	public void editAccount(int id, String firstName, String lastName,
			 String email, int phoneNumber) {
		
		try {
			initializeEditUserStatement(id,firstName, lastName, email,
					phoneNumber);			
		} catch (SQLException e) {
			logger.error("Error during create new user");
		}
		logger.info("Added: " + firstName + " " + lastName
				+ " " + email + " " + phoneNumber);
	}
	
	
	private void initializeEditUserStatement(int id,String firstName, String lastName,
			String email, int phoneNumber)
			throws SQLException {
		pstmt = super.getConnector().prepareStatement(EDIT_ACCOUNT);
		pstmt.setString(1, firstName);
		pstmt.setString(2, lastName);
		pstmt.setString(3, email);
		pstmt.setInt(4, phoneNumber);
		pstmt.setInt(5, id);
		pstmt.execute();
		pstmt.close();
	}
	
	private void initializePasswordStatement(int id,String password)
			throws SQLException {
		pstmt = super.getConnector().prepareStatement(EDIT_PASSWORD);
		pstmt.setString(1, password);
		pstmt.setInt(2, id);
		pstmt.execute();
		pstmt.close();
	}

	@Override
	public void editPassword(int id,String password) {
		try {
			initializePasswordStatement(id,password);			
		} catch (SQLException e) {
			logger.error("Error during create new user");
		}
	}

	@Override
	public void remindPassword(String email,String password) {
		try {
			initializeNewPasswordStatement(email,password);			
		} catch (SQLException e) {
			logger.error("Error during create new user");
		}
		
	}
	
	private void initializeNewPasswordStatement(String email,String password)
			throws SQLException {
		pstmt = super.getConnector().prepareStatement(REMIND_PASSWORD);
		pstmt.setString(1, password);
		pstmt.setString(2, email);
		pstmt.execute();
		pstmt.close();
	}
	
	public static void main(String[] args) {
//		EditAccount adder = new EditAccount();
//		adder.editUser(4,"Wojciech", "Sliwa", "wojtas927@o2.pl",
//				123456);
		
	}

}
